﻿/****************************************
 *  copyright (c) 2008 leo shen
 *  
 *  all rights reserved         
 * 
 *  e-mail : syl.lucky@gmail.com
 * *************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using DotUpdater.Core;

namespace DotUpdater.AppStart
{
    public partial class FrmMain : Form
    {
        Updater m_updater;
        public FrmMain(Updater updater)
        {            
            m_updater = updater;
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            this.FormClosing += new FormClosingEventHandler(FrmMain_FormClosing);
        }

        void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }    

   
        void m_updater_UpdateProgressChanged(object sender, Updater.UpdateProgressChangedEventArgs e)
        {            
            this.lvUpdateList.Items[e.UpdateItem.FileOrFolder.Name].SubItems[1].Text = e.Operate;
            this.lvUpdateList.Items[e.UpdateItem.FileOrFolder.Name].SubItems[2].Text = e.ItemProgressPercentage.ToString() + "%";
            //this.lvUpdateList.Refresh();
            this.progressBar1.Value = e.TotalProgressPercentage;
            this.lblTotalPercentage.Text = e.TotalProgressPercentage.ToString() + "%";
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {            
            foreach (UpdateItem item in m_updater.GetNeedUpdateItems())
            {
                ListViewItem lvitem = new ListViewItem(new string[] { item.FileOrFolder.Name, "准备", "0%" });
                lvitem.Name = item.FileOrFolder.Name;
                this.lvUpdateList.Items.Add(lvitem);
            }
        }

        private void btnBeginUpdate_Click(object sender, EventArgs e)
        {
            this.btnBeginUpdate.Visible = false;
            this.btnCancel.Visible = false;
            this.btnOK.Visible = true;
            this.btnOK.Enabled = false;
            this.lblStatus.Text = "正在更新";
            m_updater.UpdateProgressChanged += new Updater.UpdateProgressChangedHandler(m_updater_UpdateProgressChanged);
            m_updater.UpdateCompleted += new EventHandler(m_updater_UpdateCompleted);
            m_updater.BeginUpdate();
        }

        void m_updater_UpdateCompleted(object sender, EventArgs e)
        {
            this.lblStatus.Text = "更新完成";
            this.btnOK.Enabled = true;
            Configer.SetCurrentVersion(m_updater.CurrentVersion.Version.ToString());
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Program.OpenMainProgram();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
